﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Math;

namespace Tesla.Bounding {
    /// <summary>
    /// Represents an intersection with a bounding volume and a ray. Three cases can occur:
    /// 
    /// 1. No intersection
    /// 2. One point intersection (e.g. ray starts inside, exits)
    /// 3. Two point intersection (e.g. entry, exit points)
    /// </summary>
    public struct BoundingIntersectionRecord {
        private IntersectionRecord? _closest;
        private IntersectionRecord? _farthest;
        private int _count;

        /// <summary>
        /// Gets the number of intersections contained in the result (0, 1, or 2).
        /// </summary>
        public int IntersectionCount {
            get {
                return _count;
            }
        }

        /// <summary>
        /// Gets the closest intersection record, if it exists.
        /// </summary>
        public IntersectionRecord? ClosestIntersection {
            get {
                return _closest;
            }
        }

        /// <summary>
        /// Gets the farthest intersection intersection on the volume, if it exists.
        /// </summary>
        public IntersectionRecord? FarthestIntersection {
            get {
                //If have a first point
                if(_closest.HasValue) {
                    //If we have a second point, compare distances and return the maximum
                    if(_farthest.HasValue) {
                        if(_closest.Value.Distance > _farthest.Value.Distance) {
                            return _closest;
                        } else {
                            return _farthest;
                        }
                    }
                    //Otherwrise return the first point
                    return _closest;
                }

                //If not, doesn't matter what we return here
                return _farthest;
            }
        }

        /// <summary>
        /// Creates a new BoundingIntersectionRecord with a single intersection.
        /// </summary>
        /// <param name="record">Ray-volume Intersection</param>
        public BoundingIntersectionRecord(IntersectionRecord record) {
            _closest = record;
            _farthest = null;
            _count = 1;
        }

        /// <summary>
        /// Creates a new BoundingIntersectionRecord with two intersections.
        /// </summary>
        /// <param name="first">First ray-volume intersection</param>
        /// <param name="second">Second ray-volume intersection</param>
        public BoundingIntersectionRecord(IntersectionRecord first, IntersectionRecord second) {
            if(first.Distance <= second.Distance) {
                _closest = first;
                _farthest = second;
            } else {
                _closest = second;
                _farthest = first;
            }
            _count = 2;
        }
    }
}
